package stub;

import base.ArrayOfIntake;
import base.Intake;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.microedition.xml.rpc.Operation;
import javax.microedition.xml.rpc.Type;
import javax.microedition.xml.rpc.ComplexType;
import javax.microedition.xml.rpc.Element;
import utils.DateUtils;
import java.util.Date;

public class IntakeServicesSoap_Stub implements stub.IntakeServicesSoap, javax.xml.rpc.Stub {

    private String[] _propertyNames;
    private Object[] _propertyValues;

    public IntakeServicesSoap_Stub() {
        _propertyNames = new String[]{ENDPOINT_ADDRESS_PROPERTY};
        _propertyValues = new Object[]{utils.Config.getServerAddress() +"PublicServices/IntakeServices.asmx"};
    }

    public void _setProperty(String name, Object value) {
        int size = _propertyNames.length;
        for (int i = 0; i < size; ++i) {
            if (_propertyNames[i].equals(name)) {
                _propertyValues[i] = value;
                return;
            }
        }
        // Need to expand our array for a new property
        String[] newPropNames = new String[size + 1];
        System.arraycopy(_propertyNames, 0, newPropNames, 0, size);
        _propertyNames = newPropNames;
        Object[] newPropValues = new Object[size + 1];
        System.arraycopy(_propertyValues, 0, newPropValues, 0, size);
        _propertyValues = newPropValues;

        _propertyNames[size] = name;
        _propertyValues[size] = value;
    }

    public Object _getProperty(String name) {
        for (int i = 0; i < _propertyNames.length; ++i) {
            if (_propertyNames[i].equals(name)) {
                return _propertyValues[i];
            }
        }
        if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) || PASSWORD_PROPERTY.equals(name)) {
            return null;
        }
        if (SESSION_MAINTAIN_PROPERTY.equals(name)) {
            return new java.lang.Boolean(false);
        }
        throw new JAXRPCException("Stub does not recognize property: " + name);
    }

    protected void _prepOperation(Operation op) {
        for (int i = 0; i < _propertyNames.length; ++i) {
            op.setProperty(_propertyNames[i], _propertyValues[i].toString());
        }
    }

    //
    //  Begin user methods
    //
    public stub.PersistIntakesResponse persistIntakes(ArrayOfIntake intakes) throws java.rmi.RemoteException {
        // Copy the incoming values into an Object array if needed.
        Object[] inputObject = new Object[1];
        Object[] arrayOfIntakeObject;
        if (intakes == null) {
            arrayOfIntakeObject = null;
        } else {
            Object[] intakeArrayObject;
            if (intakes.getIntakes() == null) {
                intakeArrayObject = new Object[0];
            } else {
                intakeArrayObject = new Object[intakes.getIntakes().length];
                for (int intakeArrayIndex = 0;
                        intakeArrayIndex < intakes.getIntakes().length;
                        ++intakeArrayIndex) {
                    Object[] intakeObject;
                    if (intakes.getIntakes()[intakeArrayIndex] == null) {
                        intakeObject = null;
                    } else {
                        intakeObject = new Object[5];
                        intakeObject[0] = intakes.getIntakes()[intakeArrayIndex].getId();
                        intakeObject[1] = intakes.getIntakes()[intakeArrayIndex].getFoodId();
                        intakeObject[2] = intakes.getIntakes()[intakeArrayIndex].getUserId();
                        intakeObject[3] = new java.lang.Float(intakes.getIntakes()[intakeArrayIndex].getQuantity());
                        intakeObject[4] = DateUtils.DateToSOAP(intakes.getIntakes()[intakeArrayIndex].getDate());
                    }

                    intakeArrayObject[intakeArrayIndex] = intakeObject;
                }
            }
            arrayOfIntakeObject = new Object[1];
            arrayOfIntakeObject[0] = intakeArrayObject;
        }
        inputObject[0] = arrayOfIntakeObject;

        Operation op = Operation.newInstance(_qname_PersistIntakes, _type_PersistIntakes, _type_PersistIntakesResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://antiobesity-project.org/PersistIntakes");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException) cause;
            }
            throw e;
        }
        stub.PersistIntakesResponse result;
        // Convert the result into the right Java type.
        if (resultObj == null) {
            result = null;
        } else {
            result = new stub.PersistIntakesResponse();
        }
        return result;
    }

    public ArrayOfIntake getIntakes(Date day, String userID) throws java.rmi.RemoteException {
        // Copy the incoming values into an Object array if needed.
        Object[] inputObject = new Object[2];
        inputObject[0] = DateUtils.DateToSOAP(day);
        inputObject[1] = userID;

        Operation op = Operation.newInstance(_qname_GetIntakes, _type_GetIntakes, _type_GetIntakesResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://antiobesity-project.org/GetIntakes");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException) cause;
            }
            throw e;
        }
        ArrayOfIntake result;
        // Convert the result into the right Java type.
        // Unwrapped return value
        Object[] getIntakesResultObj = (Object[]) ((Object[]) resultObj)[0];
        if (getIntakesResultObj == null) {
            result = null;
        } else {
            result = new ArrayOfIntake();
            Intake[] intakeArray;
            Object[] intakeObj = (Object[]) getIntakesResultObj[0];
            if (intakeObj == null) {
                intakeArray = null;
            } else {
                int intakeArraySize = intakeObj.length;
                intakeArray = new Intake[intakeArraySize];
                for (int intakeArrayIndex = 0;
                        intakeArrayIndex < intakeArraySize; ++intakeArrayIndex) {
                    if (intakeObj[intakeArrayIndex] == null) {
                        intakeArray[intakeArrayIndex] = null;
                    } else {
                        intakeArray[intakeArrayIndex] = new Intake();
                        java.lang.String string;
                        Object IDObj = ((Object[]) intakeObj[intakeArrayIndex])[0];
                        string = (java.lang.String) IDObj;
                        intakeArray[intakeArrayIndex].setId(string);
                        java.lang.String string2;
                        Object foodIDObj = ((Object[]) intakeObj[intakeArrayIndex])[1];
                        string2 = (java.lang.String) foodIDObj;
                        intakeArray[intakeArrayIndex].setFoodId(string2);
                        java.lang.String string3;
                        Object userIDObj = ((Object[]) intakeObj[intakeArrayIndex])[2];
                        string3 = (java.lang.String) userIDObj;
                        intakeArray[intakeArrayIndex].setUserId(string3);
                        float a_float;
                        Object quantityObj = ((Object[]) intakeObj[intakeArrayIndex])[3];
                        a_float = ((java.lang.Float) quantityObj).floatValue();
                        intakeArray[intakeArrayIndex].setQuantity(a_float);
                        Object dateObj = ((Object[]) intakeObj[intakeArrayIndex])[4];
                        intakeArray[intakeArrayIndex].setDate(DateUtils.SOAPtoDate((String) dateObj));
                    }
                }
            }
            result.setIntakes(intakeArray);
        }
        return result;
    }

    public float getTotalCalories(Date from, Date to, String userID) throws java.rmi.RemoteException {
        // Copy the incoming values into an Object array if needed.
        Object[] inputObject = new Object[3];
        inputObject[0] = DateUtils.DateToSOAP(from);
        inputObject[1] = DateUtils.DateToSOAP(to);
        inputObject[2] = userID;

        Operation op = Operation.newInstance(_qname_GetTotalCalories, _type_GetTotalCalories, _type_GetTotalCaloriesResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://antiobesity-project.org/GetTotalCalories");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException) cause;
            }
            throw e;
        }
        float result;
        // Convert the result into the right Java type.
        // Unwrapped return value
        Object getTotalCaloriesResultObj = ((Object[]) resultObj)[0];
        result = ((java.lang.Float) getTotalCaloriesResultObj).floatValue();
        return result;
    }

    public Date getLastIntakeDate(java.lang.String userID) throws java.rmi.RemoteException {
        // Copy the incoming values into an Object array if needed.
        Object[] inputObject = new Object[1];
        inputObject[0] = userID;

        Operation op = Operation.newInstance(_qname_GetLastIntakeDate, _type_GetLastIntakeDate, _type_GetLastIntakeDateResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://antiobesity-project.org/GetLastIntakeDate");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException) cause;
            }
            throw e;
        }
        Object getLastIntakeDateResultObj = ((Object[]) resultObj)[0];
        return DateUtils.SOAPtoDate((String)getLastIntakeDateResultObj);
    }
    //
    //  End user methods
    //
    protected static final QName _qname_Date = new QName("http://antiobesity-project.org/", "Date");
    protected static final QName _qname_FoodID = new QName("http://antiobesity-project.org/", "FoodID");
    protected static final QName _qname_GetIntakes = new QName("http://antiobesity-project.org/", "GetIntakes");
    protected static final QName _qname_GetIntakesResponse = new QName("http://antiobesity-project.org/", "GetIntakesResponse");
    protected static final QName _qname_GetIntakesResult = new QName("http://antiobesity-project.org/", "GetIntakesResult");
    protected static final QName _qname_GetLastIntakeDate = new QName("http://antiobesity-project.org/", "GetLastIntakeDate");
    protected static final QName _qname_GetLastIntakeDateResponse = new QName("http://antiobesity-project.org/", "GetLastIntakeDateResponse");
    protected static final QName _qname_GetLastIntakeDateResult = new QName("http://antiobesity-project.org/", "GetLastIntakeDateResult");
    protected static final QName _qname_GetTotalCalories = new QName("http://antiobesity-project.org/", "GetTotalCalories");
    protected static final QName _qname_GetTotalCaloriesResponse = new QName("http://antiobesity-project.org/", "GetTotalCaloriesResponse");
    protected static final QName _qname_GetTotalCaloriesResult = new QName("http://antiobesity-project.org/", "GetTotalCaloriesResult");
    protected static final QName _qname_ID = new QName("http://antiobesity-project.org/", "ID");
    protected static final QName _qname_Intake = new QName("http://antiobesity-project.org/", "Intake");
    protected static final QName _qname_PersistIntakes = new QName("http://antiobesity-project.org/", "PersistIntakes");
    protected static final QName _qname_PersistIntakesResponse = new QName("http://antiobesity-project.org/", "PersistIntakesResponse");
    protected static final QName _qname_Quantity = new QName("http://antiobesity-project.org/", "Quantity");
    protected static final QName _qname_UserID = new QName("http://antiobesity-project.org/", "UserID");
    protected static final QName _qname_day = new QName("http://antiobesity-project.org/", "day");
    protected static final QName _qname_from = new QName("http://antiobesity-project.org/", "from");
    protected static final QName _qname_intakes = new QName("http://antiobesity-project.org/", "intakes");
    protected static final QName _qname_to = new QName("http://antiobesity-project.org/", "to");
    protected static final QName _qname_userID = new QName("http://antiobesity-project.org/", "userID");
    protected static final Element _type_PersistIntakes;
    protected static final Element _type_PersistIntakesResponse;
    protected static final Element _type_GetIntakes;
    protected static final Element _type_GetIntakesResponse;
    protected static final Element _type_GetTotalCalories;
    protected static final Element _type_GetTotalCaloriesResponse;
    protected static final Element _type_GetLastIntakeDate;
    protected static final Element _type_GetLastIntakeDateResponse;

    static {
        // Create all of the Type's that this stub uses, once.
        Element _type_ID;
        _type_ID = new Element(_qname_ID, Type.STRING, 0, 1, false);
        Element _type_FoodID;
        _type_FoodID = new Element(_qname_FoodID, Type.STRING);
        Element _type_UserID;
        _type_UserID = new Element(_qname_UserID, Type.STRING);
        Element _type_Quantity;
        _type_Quantity = new Element(_qname_Quantity, Type.FLOAT);
        Element _type_Date;
        _type_Date = new Element(_qname_Date, Type.STRING, 0, 1, false);
        ComplexType _complexType_intake;
        _complexType_intake = new ComplexType();
        _complexType_intake.elements = new Element[5];
        _complexType_intake.elements[0] = _type_ID;
        _complexType_intake.elements[1] = _type_FoodID;
        _complexType_intake.elements[2] = _type_UserID;
        _complexType_intake.elements[3] = _type_Quantity;
        _complexType_intake.elements[4] = _type_Date;
        Element _type_Intake;
        _type_Intake = new Element(_qname_Intake, _complexType_intake, 0, -1, true);
        ComplexType _complexType_arrayOfIntake;
        _complexType_arrayOfIntake = new ComplexType();
        _complexType_arrayOfIntake.elements = new Element[1];
        _complexType_arrayOfIntake.elements[0] = _type_Intake;
        Element _type_intakes;
        _type_intakes = new Element(_qname_intakes, _complexType_arrayOfIntake, 0, 1, false);
        ComplexType _complexType_persistIntakes;
        _complexType_persistIntakes = new ComplexType();
        _complexType_persistIntakes.elements = new Element[1];
        _complexType_persistIntakes.elements[0] = _type_intakes;
        _type_PersistIntakes = new Element(_qname_PersistIntakes, _complexType_persistIntakes);
        ComplexType _complexType_persistIntakesResponse;
        _complexType_persistIntakesResponse = new ComplexType();
        _complexType_persistIntakesResponse.elements = new Element[0];
        _type_PersistIntakesResponse = new Element(_qname_PersistIntakesResponse, _complexType_persistIntakesResponse);
        Element _type_day;
        _type_day = new Element(_qname_day, Type.STRING, 0, 1, false);
        Element _type_userID;
        _type_userID = new Element(_qname_userID, Type.STRING);
        ComplexType _complexType_getIntakes;
        _complexType_getIntakes = new ComplexType();
        _complexType_getIntakes.elements = new Element[2];
        _complexType_getIntakes.elements[0] = _type_day;
        _complexType_getIntakes.elements[1] = _type_userID;
        _type_GetIntakes = new Element(_qname_GetIntakes, _complexType_getIntakes);
        Element _type_GetIntakesResult;
        _type_GetIntakesResult = new Element(_qname_GetIntakesResult, _complexType_arrayOfIntake, 0, 1, false);
        ComplexType _complexType_getIntakesResponse;
        _complexType_getIntakesResponse = new ComplexType();
        _complexType_getIntakesResponse.elements = new Element[1];
        _complexType_getIntakesResponse.elements[0] = _type_GetIntakesResult;
        _type_GetIntakesResponse = new Element(_qname_GetIntakesResponse, _complexType_getIntakesResponse);
        Element _type_from;
        _type_from = new Element(_qname_from, Type.STRING, 0, 1, false);
        Element _type_to;
        _type_to = new Element(_qname_to, Type.STRING, 0, 1, false);
        ComplexType _complexType_getTotalCalories;
        _complexType_getTotalCalories = new ComplexType();
        _complexType_getTotalCalories.elements = new Element[3];
        _complexType_getTotalCalories.elements[0] = _type_from;
        _complexType_getTotalCalories.elements[1] = _type_to;
        _complexType_getTotalCalories.elements[2] = _type_userID;
        _type_GetTotalCalories = new Element(_qname_GetTotalCalories, _complexType_getTotalCalories);
        Element _type_GetTotalCaloriesResult;
        _type_GetTotalCaloriesResult = new Element(_qname_GetTotalCaloriesResult, Type.FLOAT);
        ComplexType _complexType_getTotalCaloriesResponse;
        _complexType_getTotalCaloriesResponse = new ComplexType();
        _complexType_getTotalCaloriesResponse.elements = new Element[1];
        _complexType_getTotalCaloriesResponse.elements[0] = _type_GetTotalCaloriesResult;
        _type_GetTotalCaloriesResponse = new Element(_qname_GetTotalCaloriesResponse, _complexType_getTotalCaloriesResponse);
        ComplexType _complexType_getLastIntakeDate;
        _complexType_getLastIntakeDate = new ComplexType();
        _complexType_getLastIntakeDate.elements = new Element[1];
        _complexType_getLastIntakeDate.elements[0] = _type_userID;
        _type_GetLastIntakeDate = new Element(_qname_GetLastIntakeDate, _complexType_getLastIntakeDate);
        Element _type_GetLastIntakeDateResult;
        _type_GetLastIntakeDateResult = new Element(_qname_GetLastIntakeDateResult, Type.STRING, 0, 1, false);
        ComplexType _complexType_getLastIntakeDateResponse;
        _complexType_getLastIntakeDateResponse = new ComplexType();
        _complexType_getLastIntakeDateResponse.elements = new Element[1];
        _complexType_getLastIntakeDateResponse.elements[0] = _type_GetLastIntakeDateResult;
        _type_GetLastIntakeDateResponse = new Element(_qname_GetLastIntakeDateResponse, _complexType_getLastIntakeDateResponse);
    }
}
